﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TerraExplorerX;
using ComponentFactory.Krypton.Toolkit;
using Bussiness;
using Entity;
using Utilities;

namespace UI
{
    public partial class ReportForm : KryptonForm
    {
        private string reportText = string.Empty;
        private int crOnFire;
        ISGWorld61 sgworld = new SGWorld61();

        CourseManager cm;
        SysConfigsManager sys;
        BestRoutesManager br;
        DataTable dt;

        public ReportForm(int crCode)
        {
            InitializeComponent();
            this.crOnFire = crCode;
        }

        private void ReportForm_Load(object sender, EventArgs e)
        {
            GenerateReport();
        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            GenerateReport();
        }
        private void GenerateReport()
        {
            reportText = string.Empty;
            
            int lastGroupID;
            string LineSeparator = "\r\n";
            SortedList currentCR;
            CourseManager cm;
            int stuCount=0;

            cm = new CourseManager();
            sys = new SysConfigsManager();
            br = new BestRoutesManager();
            dt = new DataTable();
            currentCR = new SortedList();
            cm=new CourseManager();

            lastGroupID = Convert.ToInt32(sys.GetParaByName("LastGroupID"));
            dt = br.BestRouteGetByGroupId(lastGroupID, 0);
            if (dt.Rows.Count == 0)
            {
                dt = br.BestRouteGetByGroupId(lastGroupID, 1);
            }
            reportText = reportText +"时间： "+ dt.Rows[0]["CreateTime"].ToString() + LineSeparator;
            reportText = reportText + "火灾位置: "+"知明1-"+crOnFire+LineSeparator;
            reportText = reportText+ "----------------------------"+LineSeparator;

            currentCR = cm.GetCurrentClassRooms();
            reportText = reportText + "正在上课的教室： （" + currentCR.Count + "个）" + LineSeparator;
            for (int i = 0; i < currentCR.Count; i++)
            {
                reportText = reportText + "    知明1-" + currentCR.GetKeyList()[i].ToString() + LineSeparator;
                int count=Convert.ToInt32(currentCR.GetValueList()[i]);
                stuCount = stuCount + count;
            }
            reportText = reportText + "上课人数总计： （" + stuCount + "人）" + LineSeparator;
            reportText = reportText + "----------------------------" + LineSeparator;

            DataTable dt0 = br.BestRouteGetByGroupId(lastGroupID, 0);
            reportText = reportText + "尚未疏散的教室： （" + dt0.Rows.Count + "个）" + LineSeparator;
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                string codeStr = dt0.Rows[i]["CRcode"].ToString();
                string ExitID = dt0.Rows[i]["ExitID"].ToString();
                reportText = reportText + "    知明1-" + codeStr +"（"+ExitID+"号疏散点）"+ LineSeparator;
            }

            reportText = reportText + "----------------------------" + LineSeparator;
            DataTable dt1 = br.BestRouteGetByGroupId(lastGroupID, 1);
            reportText = reportText + "已经疏散的教室： （" + dt1.Rows.Count + "个）" + LineSeparator;
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                string codeStr = dt1.Rows[i]["CRcode"].ToString();
                string ExitID = dt1.Rows[i]["ExitID"].ToString();
                reportText = reportText + "    知明1-" + codeStr + "  （" + ExitID + "号疏散点）" + LineSeparator;
            }
                

            reportText = reportText + "----------------------------" + LineSeparator;
            reportText = reportText + "各疏散点人数统计：" + LineSeparator;
            SortedList saftyList = new SortedList();
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                int courseID = Convert.ToInt32(dt0.Rows[i]["CourseID"]);
                int stuCountAtCourseID = cm.GetStuCountByCourseID(courseID);
                int ExitID = Convert.ToInt32(dt0.Rows[i]["ExitID"].ToString());                
                
                if (saftyList.Contains(ExitID))
                {
                    int indexOfKey=saftyList.IndexOfKey(ExitID);
                    int value = Convert.ToInt32(saftyList.GetByIndex(indexOfKey));
                    value += stuCountAtCourseID;
                    saftyList.SetByIndex(indexOfKey, value);
                }
                else
                {
                    saftyList.Add(ExitID, stuCountAtCourseID);
                }

            }
            IList keyList=saftyList.GetKeyList();
            IList valueList=saftyList.GetValueList();
            for (int j = 0; j < saftyList.Count; j++)
            {
                reportText = reportText +keyList[j]+"号疏散点,预计学生人数：" +valueList[j]+ LineSeparator;
            }

                reportText += "结束";
            TBox_Report.Text = reportText;
        }

        private void Btn_NoEvacuation_Click(object sender, EventArgs e)
        {
            SLHelper helper = new SLHelper();
            int slGroupId=helper.AddGroup("未疏散教室组");
            int lastGroupID = Convert.ToInt32(sys.GetParaByName("LastGroupID"));
            dt = br.BestRouteGetByGroupId(lastGroupID, 0);
            
            PropertyQuery pq = new PropertyQuery();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int crCode = Convert.ToInt32(dt.Rows[i]["CRcode"].ToString());
                EClassRoom cr = pq.Query(crCode);
                string imgPath = Application.StartupPath + @"\images\class.png";
                string str = "知明1-" + cr.CRCode.ToString();
                helper.AddImgLabel(slGroupId,cr, str, imgPath, false);
            }
        }

    }
}
